package com.gewu.pm.ui.adapter;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.ReportCategoryBean;

/**
 * 报表分类适配器
 */
public final class ReportCategoryAdapter extends AppAdapter<ReportCategoryBean> {

    private OnReportItemClickListener mItemClickListener;

    public ReportCategoryAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CategoryHolder();
    }

    public void setOnReportItemClickListener(OnReportItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    private final class CategoryHolder extends AppAdapter<?>.ViewHolder {

        private final TextView tvCategoryName;
        private final RecyclerView rvReports;
        private final ReportItemAdapter adapter;

        private CategoryHolder() {
            super(R.layout.report_category_item);
            tvCategoryName = findViewById(R.id.tv_category_name);
            rvReports = findViewById(R.id.rv_reports);

            // 设置网格布局，每行2列
            GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
            rvReports.setLayoutManager(layoutManager);

            adapter = new ReportItemAdapter(getContext());
            // 设置报表项点击监听
            adapter.setOnItemClickListener((recyclerView, itemView, position) -> {
                if (mItemClickListener != null && adapter.getData() != null) {
                    mItemClickListener.onReportItemClick(adapter.getItem(position));
                }
            });
            rvReports.setAdapter(adapter);
        }

        @Override
        public void onBindView(int position) {
            ReportCategoryBean category = getItem(position);
            tvCategoryName.setText(category.getCategoryName());
            adapter.setData(category.getReportList());
        }
    }

    public interface OnReportItemClickListener {
        void onReportItemClick(com.gewu.pm.bean.ReportItemBean item);
    }
}

